<template>
  <a-card :bordered="false" :bodyStyle="{padding:'24px 10px 24px 0px'}">
    <a-tabs default-active-key="1" tabPosition="left" @change="tabChange">
      <!-- 查询区域 -->
      <div class="table-page-search-wrapper">
        <!-- <a-form layout="inline" @keyup.enter.native="searchQuery">
          <a-row :gutter="24">
            <a-col :xl="6" :lg="7" :md="8" :sm="24" >
              <a-form-item label="流程名称">
                <a-input v-model="queryParam.modelName" placeholder="请输入流程名称"/>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24" >
              <a-form-item label="流程编码">
                <a-input v-model="queryParam.modelKey" placeholder="请输入流程编码"/>
              </a-form-item>
            </a-col>
            <a-col :xl="4" :lg="4" :md="4" :sm="24">
              <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
                <a-button  type="primary" icon="search" @click="searchQuery">查询</a-button>
                <a-button type="primary" icon="reload" style="margin-left: 8px" @click="searchReset">重置</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form> -->
      </div>
      <a-tab-pane key="1">
        <span slot="tab"><a-icon type="user" /> 未办理</span>
      </a-tab-pane>
      <a-tab-pane key="0">
        <span slot="tab"><a-icon type="clock-circle" />已办理</span>
      </a-tab-pane>
      <!-- 查询区域 -->
      <!-- <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="24">
            <a-col :xl="6" :lg="7" :md="8" :sm="24" >
              <a-form-item label="流程编码">
                <a-input v-model="queryParam.modelKey" placeholder="请输入流程编码"/>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24" >
              <a-form-item label="流程名称">
                <a-input v-model="queryParam.modelName" placeholder="请输入流程名称"/>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
                <a-button  type="primary" icon="search" @click="searchQuery">查询</a-button>
                <a-button type="primary" icon="reload" style="margin-left: 8px" @click="searchReset">重置</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div> -->
      <!-- table区域-begin -->
      <div>
        <a-table
          size="middle"
          bordered
          row-key="id"
          :columns="columns"
          :data-source="dataSource"
          :pagination="ipagination"
          :loading="loading"
          @change="handleTableChange"
        >
          <template slot="action" slot-scope="text, record">
            <a v-if="record.status>0" @click="historyClick(record)">{{[1,7].indexOf(record.status)>=0?'审批进度':'历史'}}</a>
            <a-divider type="vertical" v-if="record.status === 2 && record.reportCode" />
            <a v-if="record.status === 2 && record.reportCode" @click="openPrint(record)">打印</a>
            <a-divider type="vertical" v-if="record.status === 1" />
            <a-dropdown v-if="record.status === 1">
              <a class="ant-dropdown-link">更多<a-icon type="down" /></a>
              <a-menu slot="overlay">
                <a-menu-item >
                  <a @click="urgingClick(record)">催办</a>
                </a-menu-item>
                <a-menu-item >
                  <a-popconfirm @confirm="() => handleAbolishTask(record)" title="确定要作废流程吗?">
                    <a >作废流程</a>
                  </a-popconfirm>
                </a-menu-item>
                <a-menu-item >
                  <a-popconfirm @confirm="() => handleRevocation(record)" title="确定要取回流程吗?">
                    <a >取回流程</a>
                  </a-popconfirm>
                </a-menu-item>
              </a-menu>
            </a-dropdown>
          </template>
        </a-table>
      </div>
    </a-tabs>
    <task-modal ref="taskModal" type="history_his" :taskData="taskData" title="流程历史"></task-modal>
    <urging-modal ref="urgingModal"></urging-modal>
  </a-card>
</template>

<script>
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import taskModal from './modal/TaskModal'
import { cancellation, revocation, urging } from '@/api/task'
import { filterDictTextByCache } from '@/components/dict/JDictSelectUtil'
import { mapGetters } from 'vuex'
import { openPrint } from '@/utils/bpmn'
import { filterObj, getLikeOrEqQueryParam } from '@/utils/util'

export default {
  name: 'MyApplyProcessList',
  components: {
    taskModal
  },
  mixins: [JeecgListMixin],
  data() {
    return {
      columns: [// 表头
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function(t, r, index) {
            return parseInt(index) + 1
          }
        },
        {
          title: '业务标题',
          align: 'center',
          dataIndex: 'instanceName',
          ellipsis: true
        },
        {
          title: '流程名称',
          align: 'center',
          dataIndex: 'modelName',
          ellipsis: true
        },
        {
          title: '流程编号',
          align: 'center',
          dataIndex: 'modelKey'
        },
        {
          title: '发起人',
          align: 'center',
          dataIndex: 'creator'
        },
        {
          title: '开始时间',
          align: 'center',
          dataIndex: 'startTime'
        },
        {
          title: '结束时间',
          align: 'center',
          dataIndex: 'endTime'
        },
        {
          title: '耗时',
          align: 'center',
          dataIndex: 'totalTime'
        },
        {
          title: '状态',
          align: 'center',
          dataIndex: 'status',
          customRender: (text) => {
            return filterDictTextByCache('bpmn_status', text)
          }
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          scopedSlots: { customRender: 'action' }
        }
      ],
      url: {
        list: '/activiti/task/getMyCreateTask'
      },
      queryParam: { },
      taskData: {},
      expandParam: { active: '1' },
      excludeKeys: ['modelName', 'modelKey']
    }
  },
  methods: {
    openPrint,
    ...mapGetters(['token']),
    historyClick(record) { // 点击历史
      this.taskData = record
      this.$refs.taskModal.editModalVisible = true
    },
    urgingClick(item) { // 点击催办
      // this.$refsurgingModal.modalVisible = true
      urging({ procInstId: item.processInstanceId }).then(res => {
        if (res.success) {
          this.$message.success('催办成功')
        } else {
          this.$message.warning(res.message)
        }
      })
    },
    handleAbolishTask(record) {
      cancellation({ procInstId: record.processInstanceId }).then(res => {
        if (res.success) {
          this.loadData()
          this.$message.success('流程已作废')
        } else {
          this.$message.error(res.message)
        }
      })
    },
    handleRevocation(record) {
      revocation({
        procInstId: record.processInstanceId
      }).then(res => {
        if (res.success) {
          this.loadData()
          this.$message.success('流程已取回')
        } else {
          this.$message.warning(res.message)
        }
      })
    },
    handlePrint(data) {
      const token = this.token()
      let url = `${window._CONFIG['domianURL']}/jmreport/view/${data.reportCode}?token=${token}`
      url += `&id=${data.formDataId}&processInstanceId=${data.processInstanceId}`
      window.open(url)
    },
    tabChange(activeKey) {
      // if (activeKey === 'unhandled') {
      //   this.expandParam.active = '1'
      // } else {
      //   this.expandParam.active = '0'
      // }
      this.expandParam.active = activeKey
      this.loadData(1)
    },
    getQueryParams() {
      // 获取查询条件
      const sqp = {}
      if (this.superQueryParams) {
        sqp['superQueryParams'] = encodeURI(this.superQueryParams)
        sqp['superQueryMatchType'] = this.superQueryMatchType
      }
      const queryParam = getLikeOrEqQueryParam(this.queryParam, this.excludeKeys)
      var param = Object.assign(sqp, queryParam, this.isorter, this.filters, this.expandParam)
      param.field = this.getQueryField()
      param.pageNo = this.ipagination.current
      param.pageSize = this.ipagination.pageSize
      return filterObj(param)
    }
  }
}
</script>

<style lang="less">

</style>
